package com.travel.controller;

import com.travel.context.UserContext;
import com.travel.dto.QrCodeDto;
import com.travel.dto.TravelRegistrationDTO;
import com.travel.result.PageResult;
import com.travel.result.Result;
import com.travel.service.TicketRegistrationService;
import com.travel.service.TravelOrderInfoService;
import com.travel.vo.ContractInfoVo;
import com.travel.vo.OrderVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author: libin
 * Date: 2025/6/10 15:50
 * Description:
 **/
@Slf4j
@RestController
@RequestMapping("/travel/order")
public class TravelOrderInfoController {

    @Autowired
    private TravelOrderInfoService travelOrderInfoService;

    @Autowired
    private TicketRegistrationService ticketRegistrationService;
//    @ApiOperation("生成订单")
//    @PostMapping("/create")
//    public Result<OrderVO> create(@RequestBody Integer productId){
//        Integer userId = UserContext.getCurrentId();
//        OrderVO orderVO =  travelOrderInfoService.createOrderInfo(productId,userId);
//        return Result.success(orderVO);
//    }

    @ApiOperation("报名下单")
    @PostMapping("/register")
    public Result<Integer> register(@RequestBody TravelRegistrationDTO dto){
        // 业务逻辑
        Integer ticketId =  ticketRegistrationService.register(dto);
        return Result.success(ticketId);
    }

    @ApiOperation("支付接口")
    @GetMapping("/pay/{ticketId}")
    public Result<String> pay(@PathVariable Integer ticketId) {
        log.info("ticketId是:",ticketId);
        boolean success = ticketRegistrationService.pay(ticketId);
        return success ? Result.success() : Result.error("支付失败");

    }

    @ApiOperation("生成二维码")
    @PostMapping("/createQrCode")
    public Result<String> createQrCode(@RequestBody QrCodeDto qrCodeData) {
        String result =  ticketRegistrationService.createQrCode(qrCodeData);
        return Result.success(result);
    }

    // 扫码更新订单状态接口
    @ApiOperation("扫码下单")
    @PostMapping("/updateStatusByQrCode")
    public Result updateStatusByQrCode(@RequestBody QrCodeDto qrCodeData) {
        ticketRegistrationService.updateStatusByQrCode(qrCodeData);
        return Result.success("扫码成功");
    }


    @ApiOperation("获取订单列表")
    @GetMapping("/getOrderInfoList")
    public Result<PageResult> getOrderInfoList(@RequestParam(defaultValue = "1") Integer pageNum,
                                               @RequestParam(defaultValue = "20") Integer pageSize,
                                               @RequestParam(required = false) String status){
        return Result.success(ticketRegistrationService.getOrderInfoList(pageNum,pageSize,status));

    }

    @ApiOperation("获取订单列表管理端")
    @GetMapping("/getOrderInfoListForAdmin")
    public Result<PageResult> getOrderInfoListForAdmin(@RequestParam(defaultValue = "1") Integer pageNum,
                                               @RequestParam(defaultValue = "20") Integer pageSize,
                                               @RequestParam(required = false) String status){
        return Result.success(ticketRegistrationService.getOrderInfoListForAdmin(pageNum,pageSize,status));

    }

    @ApiOperation("获取订单详情")
    @GetMapping("/getOrderInfoById/{ticketId}")
    public Result<OrderVO> getOrderInfoById(@PathVariable Integer ticketId){
        return Result.success(ticketRegistrationService.getOrderInfoById(ticketId));
    }


    @ApiOperation("整理合同信息")
    @GetMapping("/getContractInfo/{orderId}")
    public Result<ContractInfoVo> getContractInfo(@PathVariable("orderId") Integer orderId){
        return Result.success(travelOrderInfoService.getContractInfo(orderId));
    }










}
